import streamlit as st
from streamlit_pdf_viewer import pdf_viewer

from config import HOME, FILE_FUNC, REPORT_TYPE, DICT_CONTENT


# 使用streamlit展示pdf文件内容
def display_pdf(file_path):
    with open(file_path, "rb") as f:
        pdf_data = f.read()
    pdf_viewer(pdf_data, height=600)


def section_tags():
    col1, col2, col3, col4 = st.columns(4)
    col1.selectbox("文档类型", FILE_FUNC, index=None, placeholder="文档类型")
    col2.selectbox("汇报路径", REPORT_TYPE, index=None, placeholder="汇报给谁")
    col3_rst = col3.selectbox("文档内容", DICT_CONTENT.keys(), index=None, placeholder="写的什么")
    col4_rst = col4.multiselect("", DICT_CONTENT.get(col3_rst, []), placeholder="写的什么")

    if col4_rst:
        for i in col4_rst:
            st.badge(i)


def main():
    st.title("本地化文档管理平台")

    section_tags()
    # 增加上传页面
    uploaded_file = st.file_uploader("Choose a PDF file", type=["pdf"])
    # 检查是否有文件上传
    if uploaded_file is not None:
        # 获取文件字节内容
        file_bytes = uploaded_file.read()

        # 将文件保存到本地
        save_path = f"{HOME}/{uploaded_file.name}"
        with open(save_path, "wb") as f:
            f.write(file_bytes)

        # 展示 PDF 文件
        st.subheader("PDF 文件内容")
        display_pdf(save_path)


if __name__ == "__main__":
    main()
